using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using SimpleRabbitmq;

namespace Services.Initial.Extensions;
public static class ApplicationBuilderExtensions
{
    public static IApplicationBuilder UseMyDefault(this WebApplication app,string swaggerName)
    {
        // Configure the HTTP request pipeline.
        if (app.Environment.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", $"{swaggerName}.WebAPI v1"));
            app.MapGet("/", () => Results.LocalRedirect("/swagger/index.html"));
        }
        app.MapDefaultEndpoints();
        app.RegisterRabbitmqEvent();
        //app.UseExceptionHandler();
        //app.UseEventBus();
        app.UseCors();
        //配置代理服务器，以启动负载均衡
        app.UseForwardedHeaders();
        //启动JWT
        app.UseAuthentication();
        app.UseAuthorization();
        app.MapControllers();
        return app;
    }
    public static WebApplication MapDefaultEndpoints(this WebApplication app)
    {
        // Uncomment the following line to enable the Prometheus endpoint (requires the OpenTelemetry.Exporter.Prometheus.AspNetCore package)
        // app.MapPrometheusScrapingEndpoint();

        // All health checks must pass for app to be considered ready to accept traffic after starting
        app.MapHealthChecks("/health");

        // Only health checks tagged with the "live" tag must pass for app to be considered alive
        app.MapHealthChecks("/alive", new HealthCheckOptions
        {
            Predicate = r => r.Tags.Contains("live")
        });

        return app;
    }
}